package com.ruoyi.iot.dataprocessing.controller;

import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.InnerAuth;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.iot.api.domain.HealthDatas;
import com.ruoyi.iot.api.domain.HeathParam;
import com.ruoyi.iot.dataprocessing.domain.SensorMessage;
import com.ruoyi.iot.dataprocessing.domain.WatchVO;
import com.ruoyi.iot.dataprocessing.service.ISensorMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 设备消息Controller
 * 
 * @author luogongquan
 * @date 2024-08-26
 */
@RestController
@RequestMapping("/message")
public class SensorMessageController extends BaseController
{
    @Autowired
    private ISensorMessageService sensorMessageService;

    /**
     * 查询设备消息列表
     */
    @RequiresPermissions("system:message:list")
    @GetMapping("/list")
    public TableDataInfo list(SensorMessage sensorMessage)
    {
        startPage();
        List<SensorMessage> list = sensorMessageService.selectSensorMessageList(sensorMessage);
        return getDataTable(list);
    }

    /**
     * 导出设备消息列表
     */
    @RequiresPermissions("system:message:export")
    @Log(title = "设备消息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SensorMessage sensorMessage)
    {
        List<SensorMessage> list = sensorMessageService.selectSensorMessageList(sensorMessage);
        ExcelUtil<SensorMessage> util = new ExcelUtil<SensorMessage>(SensorMessage.class);
        util.exportExcel(response, list, "设备消息数据");
    }

    /**
     * 获取设备消息详细信息
     */
    @RequiresPermissions("system:message:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sensorMessageService.selectSensorMessageById(id));
    }

    /**
     * 新增设备消息
     */
    @RequiresPermissions("system:message:add")
    @Log(title = "设备消息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SensorMessage sensorMessage)
    {
        sensorMessage.setMessage(sensorMessage.getMessageStr().getBytes());
        sensorMessageService.insertSensorMessage(sensorMessage);
        return toAjax(Boolean.TRUE);
    }

    /**
     * 修改设备消息
     */
    @RequiresPermissions("system:message:edit")
    @Log(title = "设备消息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SensorMessage sensorMessage)
    {
        return toAjax(sensorMessageService.updateSensorMessage(sensorMessage));
    }

    /**
     * 删除设备消息
     */
    @RequiresPermissions("system:message:remove")
    @Log(title = "设备消息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sensorMessageService.deleteSensorMessageByIds(ids));
    }

    @GetMapping("get")
    public AjaxResult testGet(@RequestParam Map<Object,Object> map){
        return AjaxResult.success(map);
    }
    @PostMapping("post")
    public AjaxResult testPost(@RequestBody Map<Object,Object> map){
        return AjaxResult.success(map);
    }


    @GetMapping("getLastMessageByCustomerId/{customerId}")
    public AjaxResult getLastMessageByCustomerId(@PathVariable("customerId") Long customerId){
        clearPage();
        return AjaxResult.success(sensorMessageService.getLastMessageByCustomerId(customerId));
    }

    @InnerAuth
    @GetMapping("getLastMessageByCustomerId")
    public R<WatchVO> getLastMessageByCustomerIdDate(@RequestParam("customerId")Long customerId,@RequestParam("date") String date){
        PageHelper.clearPage();
        return  R.ok(sensorMessageService.getLastMessageByCustomerIdDate(customerId,date));
    }

    @InnerAuth
    @GetMapping("/addmockData")
    R<Boolean> addmockData(@RequestParam("customerId") Long customerId){
        return R.ok(sensorMessageService.addmockData(customerId));
    }


    @GetMapping("/getHealthDataByDay/{customerId}")
    public AjaxResult getHealthDataByDay(@PathVariable Long customerId){
        return AjaxResult.success(sensorMessageService.getHealthDataByDay(customerId));
    }
    @InnerAuth
    @PostMapping("/getHealthDataInner")
    public R<HealthDatas> getHealthDataInner(@RequestBody HeathParam healthParam){
        return R.ok(sensorMessageService.getHealthData(healthParam));
    }

    @InnerAuth
    @PostMapping("/getLastWeekHealthDataInner")
    public R<HealthDatas> getLastWeekHealthDataInner(@RequestBody HeathParam healthParam){
        return R.ok(sensorMessageService.getLastWeekHealthData(healthParam));
    }
    @InnerAuth
    @PostMapping("/getLastMonthHealthDataInner")
    public R<HealthDatas> getLastMonthHealthDataInner(@RequestBody HeathParam healthParam){
        return R.ok(sensorMessageService.getLastMonthHealthData(healthParam));
    }
}
